package com.breaktian.assemble.customviews.viewpager.bannerviewpager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.breaktian.assemble.R;
import com.breaktian.assemble.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiankeke on 2017/6/15.
 */

public class PageDotView extends LinearLayout {
    public int dotSize;
    private List<ImageView> mDotViews = new ArrayList<>();
    private Context mContext;

    public PageDotView(Context context) {
        super(context);
        init(context);
    }

    public PageDotView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PageDotView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        setOrientation(HORIZONTAL);
    }

    public void setDotSize(int dotSize){
        this.dotSize = dotSize;
        addDots();
    }

    private void addDots(){
        removeAllViews();
        mDotViews.clear();
        for(int i=0;i<dotSize;i++){
            ImageView iv = new ImageView(mContext);
            iv.setImageResource(R.drawable.bm_point_normal);
            int width = ViewUtil.dip2px(mContext,8f);
            LayoutParams params = new LayoutParams(width, width);
            int margin = ViewUtil.dip2px(mContext,5f);
            params.setMargins(margin,margin,margin,margin);
            iv.setLayoutParams(params);
            addView(iv);
            mDotViews.add(iv);

        }
    }


    public void updatePosition(int pos){
        int count = getChildCount();
        for(int i=0;i<count;i++){
            View v= getChildAt(i);
            if(v instanceof ImageView){
                ImageView iv = (ImageView) v;
                if(i==pos){
                    iv.setImageResource(R.drawable.bm_point_checked);
                }else{
                    iv.setImageResource(R.drawable.bm_point_normal);
                }

            }
        }

    }



}
